package edu.asu.sefcom.extract.profile.data;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;


public class RelationalDBConnector extends SqlMapClientDaoSupport implements
		DataConnector {

	
//	public void storeUserCommData(List<String> dates, List<String> groups,
//			String userName) {
//		//create user in user master table
//		Map<String, String> map1=new HashMap<String, String>();
//		for(String groupUser:groups){
//			map1.put("USERNAME",userName);
//			map1.put("MEMBEROFGROUP",groupUser);
//			createGroupNotExists(groups);
//			Object r = getSqlMapClientTemplate().delete(
//					"edu.asu.sefcom.extract.data.ProfileData.removegroupuser", map1);
//			r = getSqlMapClientTemplate().insert(
//					"edu.asu.sefcom.extract.data.ProfileData.insertgroupuser", map1);
//			updateUser(dates,userName);
//			map1.clear();
//		}
//	}
//	private void updateUser(List<String> dates,String userName) {
//		// TODO Auto-generated method stub
//		Map<String, String> map1=new HashMap<String, String>();
//		for(String groupUser:groups){
//			map1.put("USERNAME",userName);
//			map1.put("Created",userName);
//			map1.put("Updated",userName);
//			r = getSqlMapClientTemplate().update(
//				"edu.asu.sefcom.extract.data.ProfileData.updategroupuser", map1);
//		}
//	}
//
//	private void createGroupNotExists(List<String> groups) {
//		// TODO Auto-generated method stub
//		Map<String, String> map1=new HashMap<String, String>();
//		for(String groupUser:groups){
//			map1.put("USERNAME",userName);
//			map1.put("Created",userName);
//			map1.put("Updated",userName);
//			r = getSqlMapClientTemplate().insert(
//					"edu.asu.sefcom.extract.data.ProfileData.getgroup", map1);
//			if(null==groupUser){
//		r = getSqlMapClientTemplate().insert(
//				"edu.asu.sefcom.extract.data.ProfileData.insertgroupuser", map1);
//			}
//		}
//	}
//
//	private void updateGroup(List<String> dates,String groupName) {
//		// TODO Auto-generated method stub
//		Map<String, String> map1=new HashMap<String, String>();
//		for(String groupUser:groups){
//			map1.put("USERNAME",userName);
//			map1.put("Created",userName);
//			map1.put("Updated",userName);
//			r = getSqlMapClientTemplate().update(
//				"edu.asu.sefcom.extract.data.ProfileData.updateuser", map1);
//		}
//	}
//	
//	public void storeCommData(List<String> dates, List<String> users,
//			String commName) {
//		//create group in group master table
//		Map<String, String> map1=new HashMap<String, String>();
//		for(String User:users){
//
//			createUserifNotexist(userName);
//			map1.put("GROUPNAME",commName);
//			map1.put("MEMBEROFGROUP",User);
//			//relation between member of relation user and group
//			Object r = getSqlMapClientTemplate().delete(
//					"edu.asu.sefcom.extract.data.ProfileData.removegroupuser", map1);
//			r = getSqlMapClientTemplate().insert(
//					"edu.asu.sefcom.extract.data.ProfileData.insertgroupuser", map1);
//			map1.clear();
//		}
//		updateGroup(dates, groupName);
//	}

	public void storeFollowsData(String userName, List<String> follows) {
		Map<String, String> map1=new HashMap<String, String>();
		for(String followsUser:follows){
			map1.put("USERNAME",userName);
			map1.put("FOLLOWSUSER",followsUser);
			Object r = getSqlMapClientTemplate().delete(
					"edu.asu.sefcom.extract.data.ProfileData.deleteuserfollows", map1);
			r = getSqlMapClientTemplate().insert(
					"edu.asu.sefcom.extract.data.ProfileData.insertuserfollows", map1);
			map1.clear();
		}
	}
	
	public void insertUser(ProfileData user) {
		Object r = getSqlMapClientTemplate().insert(
				"edu.asu.sefcom.extract.data.ProfileData.insertuser", user);
		logger.error("  Stored object success");
	}
	
	public void insertGroup(ProfileData user) {
		Object r = getSqlMapClientTemplate().insert(
				"edu.asu.sefcom.extract.data.ProfileData.insertgroup", user);
		logger.error("  Stored object success");
	}
	
	public void insertGroupMemberof(ProfileData user) {
		Object r = getSqlMapClientTemplate().insert(
				"edu.asu.sefcom.extract.data.ProfileData.memberof", user);
		logger.error("  Stored object success");
	}
	
	public void insertUserFollows(ProfileData user) {
		Object r = getSqlMapClientTemplate().insert(
				"edu.asu.sefcom.extract.data.ProfileData.follows", user);
		logger.error("  Stored object success");
	}

	public void createUserInfo(List<String> dates, List<String> groups,
			String userName) {
		for(String date:dates){
			Map<String,String> map=new HashMap<String,String>();
			map.put("username", userName);
			map.put("date", date);
			getSqlMapClientTemplate().insert(
					"edu.asu.sefcom.extract.data.ProfileData.insertdate", map);
		}
		for(String groupname:groups){
			Map<String,String> map=new HashMap<String,String>();
			map.put("username", userName);
			map.put("groupname", groupname);
			getSqlMapClientTemplate().insert(
					"edu.asu.sefcom.extract.data.ProfileData.insertgroup", map);
			}
	}

	public void createGroupInfo(List<String> dates, List<String> groups,
			String userName) {
		for(String date:dates){
			Map<String,String> map=new HashMap<String,String>();
			map.put("username", userName);
			map.put("date", date);
			getSqlMapClientTemplate().insert(
					"edu.asu.sefcom.extract.data.ProfileData.insertdate", map);
		}
		for(String groupname:groups){
			Map<String,String> map=new HashMap<String,String>();
			map.put("username", userName);
			map.put("groupname", groupname);
			getSqlMapClientTemplate().insert(
					"edu.asu.sefcom.extract.data.ProfileData.insertgroup", map);
			}
	}
	
	public void removeUserContent(Map<String, String> mapInfo) {
		// TODO Auto-generated method stub
		String filename = mapInfo.get("FILENAME");
		String username = mapInfo.get("USER");
		int i = getSqlMapClientTemplate().delete(
				"edu.asu.sefcom.extract.data.ProfileData.deleteFile", mapInfo);
		logger.error("  deleted return val : " + i);
	}

	public void removeGroupContent(Map<String, String> mapInfo) {
		// TODO Auto-generated method stub
		String filename = mapInfo.get("FILENAME");
		String username = mapInfo.get("USER");
		int i = getSqlMapClientTemplate().delete(
				"edu.asu.sefcom.extract.data.ProfileData.deleteFile", mapInfo);
		logger.error("  deleted return val : " + i);
	}
}
